/*********************************************************************
 *
 *  elksip - a multi-platform Sip Phone Server
 *
 *  file: elk_debug.c  debug & log
 *
 *  Copyright 2011-2014 Elksip.
 *
 ********************************************************************/

#include <stdarg.h>


#include "elk_debug.h"

static int g_level = LOG_INFO;
static FILE *g_log_file=NULL;
/*
void elk_log(int level, char *msg)
{
    if(level < g_level)
    {
        fprintf(g_log_file, "%s\n", msg);
        fflush(g_log_file);
    }
}
*/
struct 
{
    LOG_FUN_E e;
    int level;
    char desc[10];
} log_level[] =
{
    {L_F_ENCODE, LOG_INFO, ""},
    {L_F_DB, LOG_INFO, ""},
    {L_F_REG, LOG_INFO, ""},
    {L_F_UDP, LOG_INFO, ""},
    {L_F_MAIN, LOG_INFO, ""},
    {L_F_SIP, LOG_INFO, ""},
};
void elk_log(int level, LOG_FUN_E efun, const char *format, ... )
{
    if(level>log_level[efun].level)
        return;
    va_list va;
    va_start(va, format);
    vfprintf(g_log_file, format, va);
    va_end(va);
    fflush(g_log_file);

}
void elk_log_init(int log_level)
{
    if(log_level>=0 && log_level<=7)
        g_level = log_level;
    g_log_file=fopen("elksip.log","a");
    fflush(g_log_file);
}
void elk_log_close()
{
    if(g_log_file!=0)
        fclose(g_log_file);
}

void elk_debug_dump(const unsigned char *str, int len)
{
    int i=0;
    for(i=0; i<len; i++)
    {
        printf("%02x ", str[i]);
        if(i%16==15)
            printf("\n");
    }
     printf("\n");
}
